package com.ohuang.myapp.fragment.one;

import com.ohuang.baseapp.base.BaseFragment;
import com.ohuang.baseapp.utils.HiLogUtil;
import com.ohuang.baseapp.utils.ToastUtils;
import com.ohuang.baseapp.view.player.HmPlayer;
import com.ohuang.baseapp.view.player.api.ImplPlayer;
import com.ohuang.baseapp.view.player.constant.Constants;
import com.ohuang.baseapp.view.player.view.PlayerLoading;
import com.ohuang.baseapp.view.player.view.PlayerView;
import com.ohuang.baseapp.view.player.view.SimplePlayerController;
import com.ohuang.myapp.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.hiviewdfx.HiLog;

public class VideoFragment extends BaseFragment {
    @Override
    protected int getXmlId() {
        return ResourceTable.Layout_fg_video;
    }

    private PlayerView playerView;
    private PlayerLoading loadingView;
    private SimplePlayerController controllerView;
    private String url = "entry/resources/base/media/gubeishuizhen.mp4";
    private ImplPlayer player;
    private boolean isPlay;
    private boolean isVisible;

    @Override
    protected void initComponent() {
        HiLogUtil.debug("VideoFragment", "initComponent");
        player = new HmPlayer.Builder(getContext()).setFilePath(url).create();
        player.getLifecycle().onStart();
        playerView = (PlayerView) findComponentById(ResourceTable.Id_player_view);
        loadingView = (PlayerLoading) findComponentById(ResourceTable.Id_loading_view);
        controllerView = (SimplePlayerController) findComponentById(ResourceTable.Id_controller_view);

        playerView.bind(player);
        loadingView.bind(player);
        controllerView.bind(player);
    }


    @Override
    public void userVisibleChange(boolean a) {

        HiLogUtil.debug("VideoFragment", "setUserVisibleChange" + a);
        if (!isVisible && a) {
            if (!isPlay) {
                getContext().getGlobalTaskDispatcher(TaskPriority.DEFAULT).delayDispatch(() -> player.play(), Constants.NUMBER_1000);
                isPlay = true;
            }
            player.getLifecycle().onForeground();
        } else if (isVisible && !a) {
            player.getLifecycle().onBackground();
        }
        isVisible = a;
    }



    @Override
    protected void onForeground() {
        if (isVisible) {
            HiLogUtil.debug("VideoFragment","onActive");
            getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
                @Override
                public void run() {
                    player.getLifecycle().onForeground();
                }
            });
        }
        super.onForeground();

    }

    @Override
    protected void onBackground() {
        if (isVisible) {
            player.getLifecycle().onBackground();
        }
        super.onBackground();
    }


    @Override
    protected void onDestroyComponent() {
        super.onDestroyComponent();
        HiLogUtil.debug("VideoFragment", "onDestroyComponent");
        loadingView.unbind();
        controllerView.unbind();
        playerView.unbind();
        player.getLifecycle().onStop();
        isPlay = false;
        isVisible = false;

    }

    @Override
    protected void onStop() {

        super.onStop();
    }
}
